﻿layui.config({
    base: "/Resource/js/modules/"
}).use(['jax', 'table'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$;
    var layer = parent.layer || layui.layer;

    table.render({
        elem: '#container'
        , id: 'tableList'
        , url: '/Admin/GetPageing'
        , response: {
            statusName: 'status',
            statusCode: 200,
            msgName: 'message'
        }
        , cellMinWidth: 80
        , cols: [[
        { checkbox: true }
        , { field: 'RealName', title: '姓名', align: 'center' }
        , { field: 'LoginName', title: '登录名', align: 'center' }
        , { field: 'SchoolName', title: '学校', align: 'center' }
        , { field: 'GradeName', title: '年级', align: 'center' }
        , { field: 'ClassName', title: '班级', align: 'center' }
        , { field: 'Sex', title: '性别', align: 'center', templet: '<div>{{d.Sex==1?"男":"女"}}</div>' }
        , { title: '用户状态', templet: '#stateTpl', align: 'center' }
        , { title: '操作', align: 'center', toolbar: '#tableBar' }
        ]]
        , page: true
    });

    table.on('tool(tableList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        switch (layEvent) {
            case 'detail':
                layer.open({
                    type: 2,
                    title: '查看',
                    shade: 0.5,
                    area: ['403px', '400px'],
                    content: ['/Admin/GetUser?id=' + data.UserId, 'no'] //iframe的url，no代表不显示滚动条
                });
                break;
            case 'del':
                User.dele(obj);
                break;
            case 'edit':
                obj.update({
                    LoginName: '123'
                , title: 'xxx'
                });
                break;
        }
    });
    $('#queryPanel').on('click', 'a', function () {
        var Event = $(this).attr('event');
        switch (Event) {
            case 'search':
                User.search();
                break;
            case 'add':
                break;
            case 'del':
                User.deleAll();
                break;
        }
    })
    form.on('switch(userstate)', function (obj) {
        User.status(this.value);
    });

    var User = {
        //查询
        search: function () {
            var realName = $('#realName').val().trim();
            var loginName = $('#loginName').val().trim();
            var schoolId = $("#school").val() || 0;
            table.reload('tableList', {
                where: {
                    realName: realName,
                    loginName: loginName,
                    schoolId: schoolId
                },
                page: {
                    curr: 1
                }
            });
        },
        //更新状态
        status: function (id) {
            $.post('/Admin/SetState', { id: id }, function (data) {
                if (data.status == 200) {
                    layer.msg('操作成功', { time: 800 });
                } else {
                    layer.msg('更新失败，' + data.message, { icon: 15, time: 1200, shade: 0 });
                }
            })
        },
        //删除
        dele: function (obj) {
            layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
                layer.close(index);
                //向服务端发送删除指令
                $.post("/Admin/Delete", { id: obj.data.UserId }, function (data) {
                    if (data.status == 200) {
                        obj.del(); 
                        layer.msg('删除成功', { time: 1000 });
                    } else {
                        //
                        layer.msg('删除失败,' + data.message + '', { icon: 15, time: 1200, shade: 0 });
                    }
                })
            });
        },
        //批量删除
        deleAll: function () {
            var checkStatus = table.checkStatus('tableList');
            if (checkStatus.data.length === 0) {
                layer.msg('请选择需要删除的数据', { time: 1020 });
            } else {
                var ids = function () {
                    var data = checkStatus.data;
                    var array = new Array();
                    $.each(data, function (key, value) {
                        array.push(value["UserId"]);
                    })
                    return array;
                }();
                layer.confirm('确定要删除选中的数据吗？', { icon: 3, title: '提示' }, function (index) {
                    layer.close(index);
                    //向服务端发送删除指令
                    $.post("/Admin/DeleteAll", { id: ids }, function (data) {
                        if (data.status == 200) {
                            layer.msg('删除成功', { time: 1000 });
                            if (table.cache['tableList'].length === ids.length) {
                                //如果一次都删完 就重载一下
                                table.reload('tableList', {});
                            } else {
                                $('.layui-form-checked').each(function () {
                                    var tr = $(this).closest('tr');
                                    var index = tr.index();
                                    tr.remove();
                                    table.cache['tableList'][index] = [];
                                })
                            }
                        } else {
                            layer.msg('删除失败,' + data.message + '', { icon: 15, time: 1200, shade: 0 });
                        }
                    })
                });
            }
        }
    }
});